<template>
	<div class="radio">
		<div v-if="initloading" class="radio-loading">
			<el-icon class="is-loading"><el-icon-loading /></el-icon>
		</div>
		<el-radio-group v-bind="$attrs" v-model="value" :loading="loading" @visible-change="visibleChange">
			<el-radio v-for="item in options" :key="item[props.value]" :label="item[props.value]" :value="item[props.value]">
				{{item[props.label]}}
			</el-radio>
		</el-radio-group>
	</div>
</template>

<script>
	import config from "@/config/select";

	export default {
		props: {
			apiObj: { type: Object, default: () => {} },
			dict: { type: String, default: "" },
			params: { type: Object, default: () => ({}) },
		},
		data() {
			return {
				dicParams: this.params,
				loading: false,
				options: [],
				radio: "",
				props: config.props,
				initloading: false,
				value: undefined
			}
		},
		computed:{
			value: function(){
				return this.$attrs.modelValue.toString();
			}
		},
		created() {
			this.initloading = true;
			this.getRemoteData();
		},
		methods: {
			visibleChange(ispoen){
				if(ispoen && this.options.length==0 && (this.dict || this.apiObj)){
					this.getRemoteData()
				}
			},
			async getRemoteData(){
				this.loading = true
				var res = {}
				if(this.apiObj){
					res = await this.apiObj.get(this.params)
				}else if(this.dict){
					this.props = { label: 'dictLabel', value: 'dictValue' }
					res = await config.dicApiObj.get(this.dict)
				}
				var response = config.parseData(res)
				this.options = response.data
				this.loading = false
				this.initloading = false
			}
		}
	}
</script>

<style scoped>
	.radio {display: inline-block;position: relative;}
	.radio-loading {position: absolute;top:0;left:0;right:0;bottom:0;background: #fff;z-index: 100;border-radius: 5px;border: 1px solid #EBEEF5;display: flex;align-items: center;padding-left:10px;}
	.radio-loading i {font-size: 14px;}

	.dark .radio-loading {background: var(--el-bg-color-overlay);border-color: var(--el-border-color-light);}
</style>
